package com.auto.learning.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;

import com.auto.learning.MainActivity;
import com.auto.learning.R;
import com.auto.learning.adapter.CommonAdapter;
import com.auto.learning.adapter.ViewHolder;
import com.auto.learning.tools.CommonMethod;

public class SmoothBookListFragment extends Fragment {

	private GridView gv_booklist;
	private List<BookListBean> list = new ArrayList<BookListBean>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = LayoutInflater.from(getActivity()).inflate(R.layout.smooth_book_list_fragment, null);
		initData();
		initView(view);
		return view;
	}
	
	private void initData(){
		list.add(new BookListBean(1, R.drawable.smooth_1, "本月最火"));
		list.add(new BookListBean(2, R.drawable.smooth_2, "好评Top5"));
		list.add(new BookListBean(3, R.drawable.smooth_3, "烧脑干货"));
		list.add(new BookListBean(4, R.drawable.smooth_4, "暖心有爱"));
	}
	
	private void initView(View view){
		gv_booklist = (GridView) view.findViewById(R.id.gv_booklist);
		gv_booklist.setAdapter(new CommonAdapter<BookListBean>(getActivity(),list, R.layout.smooth_item_layout) {

			@Override
			public void convert(ViewHolder viewHolder, BookListBean item) {
				viewHolder.setText(R.id.tv_title, item.title+"");
				viewHolder.setVisible(R.id.tv_title, false);
				ImageView imageView = viewHolder.getView(R.id.image_book);
				imageView.setImageBitmap(CommonMethod.readBitMap(getActivity(), item.getResId()));
			}
		});
		gv_booklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(getActivity(),MainActivity.class);
				intent.putExtra("gotoBookList", list.get(position).getId());
				startActivity(intent);
				getActivity().finish();
			}
		});
		view.findViewById(R.id.linear_buttom).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(),MainActivity.class);
				startActivity(intent);
				getActivity().finish();
			}
		});
	}
	
	class BookListBean{
		private int id;
		private int resId;
		private String title;
		public BookListBean(int id, int resId, String title) {
			super();
			this.id = id;
			this.resId = resId;
			this.title = title;
		}
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public int getResId() {
			return resId;
		}
		public void setResId(int resId) {
			this.resId = resId;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
	}
}

